//
//  ToDo+Alamofire.swift
//  RX
//
//  Created by iOS开发 on 2019/2/14.
//  Copyright © 2019 iOS开发. All rights reserved.
//

import Foundation
import Alamofire
import RxSwift

extension ToDo {
    class func getList(route : Router) -> Observable<[[String : Any]]> {
        return Observable.create{ observer ->Disposable in
            let request = Alamofire.request(Router.get(nil).asURLRequest())
                .responseJSON { (response) in
                    guard response.result.error == nil else{
                        observer.onError(response.result.error!)
                        return
                    }
                    
                    guard let todos = response.result.value as? [[String : Any]]  else {
                        observer.onError(response.result.error!)
                        return
                    }
                    
                    observer.onNext(todos)
                    observer.onCompleted()
            }
            return Disposables.create {
                request.cancel()
            }
        }
    }
}
